window.onload = function() {

    var tran = document.getElementsByClassName("tran ");
    var logging = document.getElementsByClassName("logging");
    for (var i = 0; i < tran.length; i++) {
        tran[i].num = i;
        tran[i].addEventListener("click", function() {
            var index = this.num;
            // 设置类让选中的页面透明
            logging[index].className = "logging hidden";
            var time = setTimeout(function() {
                // 让选中的页面在淡入后消失
                logging[index].style["display"] = "none";
                // 让另一个页面淡出
                logging[1 - index].className = "logging";
            }, 300);
            // 另一个页面淡出后出现
            logging[1 - index].style["display"] = "block";
        })
    }

    //查看动态部分
    var submit33 = document.getElementById("submit33");
    // var name = username.value;
    // var key = password.value;
    // var ID = id.value;
    submit2.onmousedown = function() {

        ajax({

            type: 'post',
            dataType: 'json',
            url: 'http://www.rushmc.top/api/dynamicState/insert',
            data: {
                "id": 421,
                "userId": 1,
                "username": "bb",
                "content": "群除我佬",
                "title": "过来玩玩，大佬们等等我",
                "creationTime": "2021-04-25T22:50:39.000+00:00",
                "hasPicture": 1,
                // "urls": ["2.jpg", "3.jpg"]
            },
            success: function(result) {
                // 检测接口有没有连成功
                if (result.code == 200) {
                    // submit2.innerHTML = '返回成功';
                    submit2.className = 'sb sb-success';
                    // result.data;
                    console.log('success');
                } else {
                    console.log('error');
                }
            }
        })
    }

    // 向服务端发送请求
    submit33.onmousedown = function() {
            ajax({

                type: 'get',
                dataType: 'json',
                url: 'http://www.rushmc.top/api/dynamicState/select/all',
                data: {

                },
                success: function(result) {
                    // 检测接口有没有连成功
                    if (result.code == 200) {
                        console.log('返回成功');
                        // submit33.className = 'sb sb-success';
                        console.log('success');
                        var demo = document.getElementById('demo');
                        var content = document.querySelector('.content');
                        var content1 = document.querySelector('.content1');
                        var smallbox = document.querySelector('.smallbox');
                        var submit66 = document.querySelector('#submit33');
                        var detail = document.querySelector('.detail');
                        submit66.addEventListener('click', function() {
                            content.style.display = 'block';
                            smallbox.style.display = 'none';

                        })

                        for (let i = 0; i < result.data.length; i++) {

                            var div = document.createElement("div");
                            div.id = "slide";
                            div.innerHTML = '<div class="de"><button class="detail">查看详情</button><div class="zi0"><h5 class="ii">用户id: ' + result.data[i].userId + '</h5><h5 class="ii">用户名字：' + result.data[i].username + '</h5><h5 class="ii">动态id：' + result.data[i].id + '</h5></div><li class="time">发布时间：' + result.data[i].creationTime + '</li><li class="zi">' + result.data[i].title + '</li><h5 class="zi1">发布内容：' + result.data[i].content + '......'
                            '</h5></div>'
                            // `<div>${result.data[i].id}</div>
                            //  <b>${result.data[i].userId}<b>
                            //  <b>${result.data[i].username}<b>
                            //  <b>${result.data[i].content}<b>
                            //  <br>${result.data[i].title}<br>`
                            // UL.appendChild(LI);
                            // odiv.appendChild(UL);
                            demo.appendChild(div);
                            detail.addEventListener('click', function() {
                                content.style.display = 'none';
                                content1.style.display = 'block';
                                document.getElementById("demo1").innerHTML = result.data[i].value;

                            })


                        }


                    } else {
                        console.log('返回错误');

                    }
                },
            })
        }
        // 获取页面中的元素
    var username = document.getElementById("username");
    var info = document.getElementById("info");
    var password = document.getElementById("password");
    var submit = document.getElementById("submit");

    submit.onmousedown = function() {
            // 获取用户输入的用户名
            var name = username.value;
            var key = password.value;

            // 向服务端发送请求
            ajax({
                type: 'post',
                url: 'http://www.rushmc.top/api/login',
                data: {
                    username: name,
                    password: key
                },
                success: function(result) {
                    // 检测登录有没有成功
                    if (result.code == 200) {
                        submit.innerHTML = '登录成功';
                        submit.className = 'sb sb-success';
                        // username.value = '';
                        // password.value = '';

                        setTimeout(function() {
                            var logging = document.querySelector('.loginarea');
                            var container111 = document.querySelector('.loginarea1');
                            var submit7 = document.querySelector('.closebtn1');
                            submit7.addEventListener('click', function() {
                                    logging.style.display = 'none';
                                    container111.style.display = 'block';

                                })
                                // window.location('success.html');
                            console.log('结束');
                        }, 500)

                        console.log(result.data);

                    } else {
                        submit.innerHTML = '账号或密码错误';
                        submit.className = 'sb sb-error';
                        // 重新登录的时候把搜索框调为空
                        setTimeout(function() {
                            username.value = '';
                            password.value = '';
                            submit.innerHTML = '登录';
                            submit.className = 'submit';
                            console.log('清空！！');
                        }, 2000)
                    }
                },
                // error: function(result) {
                //     console.log(result);
                //     info.className = 'bg-danger'
                // }
            })
        }
        // 获取文本框和按钮
    var username_1 = document.getElementById("username_1");
    var password_1 = document.getElementById("password_1");
    var name_1 = document.getElementById("name_1");
    var phone_1 = document.getElementById("phone_1");
    var submit_1 = document.getElementById("submit_1");
    password_1.onblur = function() {
        if (this.value.length < 6 || this.value.length > 16) {
            document.getElementById("ti2").innerHTML = "（×）您输入的密码不符合6~16位";
            ti2.className = 'ti2 wrong';
            setTimeout(function() {
                document.getElementById("ti2").innerHTML = "";
                console.log('清空！！');
            }, 2000)
        } else {
            document.getElementById("ti2").innerHTML = "（√）您输入的密码格式正确";
            ti2.className = 'ti2 right1';
            setTimeout(function() {
                document.getElementById("ti2").innerHTML = "";
                console.log('清空！！');
            }, 2000)
        }
    }
    username_1.onblur = function() {
        if (this.value.length > 6) {
            document.getElementById("ti2").innerHTML = "（×）您输入的用户名要小于6位";
            ti2.className = 'ti2 wrong';
            setTimeout(function() {
                document.getElementById("ti2").innerHTML = "";
                console.log('清空！！');
            }, 2000)
        } else if (this.value.length == 0) {
            document.getElementById("ti2").innerHTML = "（×）您输入的用户名不能为空";
            ti2.className = 'ti2 wrong';
            setTimeout(function() {
                document.getElementById("ti2").innerHTML = "";
                console.log('清空！！');
            }, 2000)
        } else {
            document.getElementById("ti2").innerHTML = "（√）您输入的用户名格式正确";
            ti2.className = 'ti2 right1';
            setTimeout(function() {
                document.getElementById("ti2").innerHTML = "";
                console.log('清空！！');
            }, 2000)
        }
    }
    phone_1.onkeyup = function() {
        var num1 = phone_1.value;
        // 验证电话的正则表达式
        var reg = /^1[34578][0-9]{9}$/;
        // 如果用户输入的电话不符合规则
        if (!reg.test(num1)) {
            console.log("不符合格式");
            info.innerHTML = '请输入符合样式的号码';
            // 让提示信息显示为错误的提示信息样式
            info.className = 'bg bg-error';
            // 阻止程序向下执行
            return;
        } else {
            // 如果电话输入正确提示正确信息，并设置相应样式
            info.innerHTML = '格式正确';
            info.className = 'bg bg-success';
        }

        submit_1.onclick = function() {
            var une1 = username_1.value;
            var key1 = password_1.value;
            var un1 = name_1.value;
            var num1 = phone_1.value;

            ajax({
                type: 'post',
                url: 'http://www.rushmc.top/api/register',
                data: {
                    username: une1,
                    password: key1,
                    name: un1,
                    phone: num1
                },
                success: function(result) {
                    console.log(result.msg);
                    // 判断是否注册成功
                    if (result.code == 200) {
                        submit_1.innerHTML = '注册成功，请返回登录界面';
                        submit_1.className = 'sb sb-success';
                        username.value = '';
                        password.value = '';
                    } else {
                        submit_1.innerHTML = '账号已存在,重新注册';
                        submit_1.className = 'sb sb-error';
                        setTimeout(function() {
                            username_1.value = '';
                            password_1.value = '';
                            name_1.value = '';
                            phone_1.value = '';
                            submit_1.className = 'submit';
                            submit_1.innerHTML = '注册';
                            console.log('清空！！');
                        }, 2000)


                    }

                },
                // error: function(result) {
                //     console.log(result);
                //     submit_1.innerHTML = '注册失败';
                //     submit_1.className = 'bg-danger';
                // }
            })
        }
    }

}